package com.zjj.algorithm.learning.atguigu.recursion;

import java.util.*;

/**
 * @author zjj_admin
 * @date 2022/11/12 12:49
 */
public class MiGong {

    public static void main(String[] args) {
        //设置一个地图
        int[][] map = new int[8][7];

        //将第一列和最后一列设置为 1
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }

        //将第一行和最后一行设置为 1
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        map[3][1] = 1;
        map[3][2] = 1;
        map[4][3] = 1;
        map[5][3] = 1;

        map[2][3] = 1;
        map[3][3] = 1;

//        map[1][2] = 1;
//        map[2][2] = 1;

        //打印地图
        System.out.println("地图为：");
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + "  ");
            }
            System.out.println();
        }

//        boolean res = setWay(map, 1, 1);
//        System.out.println("有通路吗？" + res);
//        System.out.println("走过之后的地图为：");
//
//        for (int i = 0; i < map.length; i++) {
//            for (int j = 0; j < map[i].length; j++) {
//                System.out.print(map[i][j] + "  ");
//            }
//            System.out.println();
//        }

        List<List<Integer>> lists = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                for (int k = 1; k <= 4; k++) {
                    for (int l = 1; l <= 4; l++) {
                        if (i != j && i != k && i != l && j != k && j != l && k != l) {
                            List<Integer> list = new ArrayList();
                            list.add(i);
                            list.add(j);
                            list.add(k);
                            list.add(l);
                            lists.add(list);
                        }
                    }
                }
            }
        }


        List<Integer> minPathStrategy = new ArrayList<>();
        int[][] minPathMap = new int[0][];
        int minPath = 8 * 7;

        int count = 0;
        for (List<Integer> strategy : lists) {
            int path = 0;
            Map<String, Object> res = setWay(map, 1, 1, strategy);
            if (res.get("isSuccess").equals(Boolean.TRUE)) {
                count ++;
                int[][] availableMap = (int[][]) res.get("data");
                for (int i = 0; i < availableMap.length - 1; i++) {
                    for (int j = 0; j < availableMap[i].length - 1; j++) {
                        if (availableMap[i][j] == 2) {
                            path++;
                        }
                    }
                }
                if (minPath > path) {
                    minPath = path;
                    minPathMap = availableMap;
                    minPathStrategy = strategy;
                }
            }
        }
        System.out.println("count = " + count);


        System.out.println("minPath = " + minPath);
        System.out.println("minPathStrategy = " + minPathStrategy.toString());
        System.out.println("最短可选路线为：");
        for (int i = 0; i < minPathMap.length; i++) {
            for (int j = 0; j < minPathMap[i].length; j++) {
                System.out.print(minPathMap[i][j] + "  ");
            }
            System.out.println();
        }


    }



    /**
     * 规则：
     * 1、迷宫的起点为 map[1][1]，终点为 map[6][5]。
     * 2、地图中，坐标数据为 0，表示这个点没有走过，数据为 1，表示为墙，数据为 2 表示已经经过这个点了，数据为 3 表示这个点是死路，不能走
     * 3、行走的规则为 下 -》右-》上-》左
     *
     * @param map
     * @param i
     * @param j
     * @return
     */
    public static boolean setWay(int[][] map, int i, int j) {
        if (map[6][5] == 2) {
            return true;
        }
        if (map[i][j] == 0) {
            map[i][j] = 2;
            //先向右
            if (setWay(map, i + 1, j)) {
                return true;
            } else if (setWay(map, i, j + 1)) {
                return true;
            } else if (setWay(map, i - 1, j)) {
                return true;
            } else if (setWay(map, i, j - 1)) {
                return true;
            } else {
                //将这个点设置为 3，是死路
                map[i][j] = 3;
                return false;
            }
        } else {
            //当 map[i][j] != 0 时，1,2,3则都返回false
            return false;
        }

    }


    /**
     * 此方法未完成
     * @param map
     * @param i
     * @param j
     * @param strategy strategy 是一个大小为 4 的数组 行走的策略，里面存储 1,2,3,4
     *                 其中 1 表示上，2表示下，3表示左，4表示右
     *                 strategy 中的数据 可能有 1,2,3,4；2,1,3,4；...等等等共24中组合
     * @return
     */
    public static Map<String, Object> setWay(int[][] map, int i, int j, List<Integer> strategy) {
        Map<String, Object> res = new HashMap<>();
        res.put("data", map);
        if (map[6][5] == 2) {
            res.put("isSuccess", Boolean.TRUE);
            return res;
        }
        if (map[i][j] == 0) {
            map[i][j] = 2;
            //先向右
            for (int k = 0; k < strategy.size(); k++) {
                map[i][j] = 2;
                if (strategy.get(k) == 1) {
                    if (setWay(map, i - 1, j,strategy).get("isSuccess").equals(Boolean.TRUE)) {
                        res.put("isSuccess", Boolean.TRUE);
                        return res;
                    }
                } else if (strategy.get(k) == 2) {
                    if (setWay(map, i + 1, j,strategy).get("isSuccess").equals(Boolean.TRUE)) {
                        res.put("isSuccess", Boolean.TRUE);
                        return res;
                    }
                } else if (strategy.get(k) == 3) {
                    if (setWay(map, i, j - 1,strategy).get("isSuccess").equals(Boolean.TRUE)) {
                        res.put("isSuccess", Boolean.TRUE);
                        return res;
                    }
                } else {
                    if (setWay(map, i, j + 1,strategy).get("isSuccess").equals(Boolean.TRUE)) {
                        res.put("isSuccess", Boolean.TRUE);
                        return res;
                    }
                }
                map[i][j] = 3;
                res.put("isSuccess", Boolean.FALSE);
                return res;
            }
        } else {
            //当 map[i][j] != 0 时，1,2,3则都返回false
            res.put("isSuccess", Boolean.FALSE);
            return res;
        }
        return null;
    }
}
